<template>
  <div>
    <ul>
      <!-- 1. 标签式跳转 -->
      <!-- router-link不适用内部有复杂的结构 -->
      <!-- router-link  query传参 拼接 -->
      <!-- <router-link :to="'/detail?item=' + item" v-for="item in list" tag="li" :key="item.id">
        <img style="width: 100px" :src="item.img" alt="" />
        <span>
          {{ item.title }}
        </span>
      </router-link> -->

      <!-- router-link  query传参 配置对象 -->
      <!-- <router-link :to="{
         path: '/detail',
         query: {
           item
         }
       }" v-for="item in list" tag="li" :key="item.id">
        <img style="width: 100px" :src="item.img" alt="" />
        <span>
          {{ item.title }}
        </span>
      </router-link> -->

      <!-- 2. 编程式跳转 -->
      <li v-for="item in list" @click="pushDetail(item.id)" :key="item.id">
        <img style="width: 100px" :src="item.img" alt="" />
        <span>
          {{ item.title }}
        </span>
        <button>+++</button>
      </li>
    </ul>
  </div>
</template>

<script>
// [/detail]
// ['baidu', 'http://localhost:8080/']
export default {
  data() {
    return {
      list: [
        {
          id: 111,
          title: "我是商品1",
          img: "https://img.dmallcdn.com/20190613/671cff4a-7dd3-43c9-b0d1-b1b2ff46d56b_480x480H",
        },
        {
          id: 112,
          title: "我是商品2",
          img: "https://img.dmallcdn.com/prod/163/20200923/401a4c1be82d4b5c9d05995adc8b10a0_480x480H",
        },
        {
          id: 113,
          title: "我是商品3",
          img: "https://img.dmallcdn.com/prod/163/20210826/69ec290376524551ba27796ea921070c_480x480H",
        },
      ],
    };
  },
  methods: {
    pushDetail(id) {
      console.log(this.$router)
      // push (浏览器有记录)
      // this.$router.push('/detail?id=' + id)
      // replace  (浏览器无记录)
      // this.$router.replace('/detail?id=' + id)

      this.$router.push({
        // 跳转的url
        path: "/detail",
        // url可见参数
        query: {
          id
        }
      })
    }
  }
};
</script>

<style>
</style>